package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by Wuxiang on 2018/4/10
 */
public class TestServlet extends HttpServlet{

    /**
     * Constructor of the object.
     */
    public TestServlet() {
        System.out.println("TestServlet 构造函数");
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy();
        System.out.println("TestServlet 销毁");
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //请求重定向方式跳转到test.jsp,使用request.getContextPath获得上下文对象
        response.sendRedirect(request.getContextPath()+"/test.jsp");

        //服务器内部跳转,这里的斜线表示项目的根目录
//        request.getRequestDispatcher("/test.jsp").forward(request, response);
//        request.getRequestDispatcher("../test.jsp").forward(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        System.out.println("TestServlet init初始化");
    }
}
